Lab5


4531210121_4531214721  น.ส. ฐิติพร วงศ์ไพศาลเจริญ และ น.ส. ธัชนันท์ เตชะสมบูรณากิจ (13/8/2545 (11:31:11))
(SM=2, CM=4, ST=5, KY=0, TR=08:48)

TestScript
Mini-Quiz :  (0.0 คะแนน)

JLab>java Selftest
>>JLabIO->Testing (2014, 11) : 2.00
>>JLabIO->Testing (2020, 12) : 2.00
>>JLabIO->Testing (1985, 2) : 2.00
>>JLabIO->Testing (2014, 12) : 2.00
>>JLabIO->Testing (1994, 6) : 2.00

>>JLab:<POINT>10.00</POINT>
JLab>

ได้ 10 คะแนน
Source Code
/**
 * 2110101 Computer Programming
 * Lab #5 : Loops : while, do-while, for
 */
import java.util.*;
import jlab.JLabIO;

public class Lab5 {
  public static void calendar(int year, int month) {
    // Example
    //-----------------------------
    //        August(2002)
    //-----------------------------
    // SUN MON TUE WED THU FRI SAT
    //                   1   2   3     \
    //   4   5   6   7   8   9  10      |
    //  11  12  13  14  15  16  17       >  your code
    //  18  19  20  21  22  23  24      |
    //  25  26  27  28  29  30  31     /
    //=============================
    
    // print header
    System.out.println(LONGLINE);
    System.out.println("        " + monthName[month - 1] + "(" + year + ")");
    System.out.println(LONGLINE);
    System.out.println(" SUN MON TUE WED THU FRI SAT");
    
    Calendar calendar = new GregorianCalendar(new Locale("th", "TH"));
    calendar.set((year > 2500 ? year - 543 : year), month - 1, 1);
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    int numDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    
    // add your code here to format and print the month
    // Use the following two variables :
    //   - numDays is the number of days in this month
    //   - firstDay is the day-of-week of the first of this month
    
 int x, j;
 String s = "    ";
 x = firstDay;
 for (j = 1; j < firstDay; j++)
  System.out.print(s);
 for (int i = 1; i <= numDays; i++) {
   if (x <= 7) {
     System.out.print(JLabIO.format(i, 4));
     x++;
     }
   else {
       System.out.println();
       System.out.print(JLabIO.format(i, 4));
       x = 2;
     }
}
       System.out.println();





    
    // add a line to notify the end of printing
    System.out.println(LASTLINE);
  }
  
  //----------------------------------------------------------------
  public static void main(String[] args) {
    int y = JLabIO.readInt("Year = ");
    int m = JLabIO.readInt("Month = ");

    if (y > 0 && (1 <= m && m <= 12)) {
      calendar(y, m);
    } else {
      System.out.println("error : invalid year or month");
    }
  }
  //----------------------------------------------------------------
  
  final static String[] monthName =
     {"January", "February", "March", "April",
      "May", "June", "July", "August",
      "September", "October", "November", "December"};
  final static String LONGLINE = "-----------------------------";
  final static String LASTLINE = "=============================";
  
}